package ni;

/*
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import signals.FileProposal;
import signals.FileTransferNOK;
import signals.FileTransferOK;
import signals.Signal;
import signals.SignalTooBigException;
*/

/**
 * This class used to be used when we were doing the file transfer negociation in TCP
 * This class IS NOT USED ANYMORE but we did not have the heart to delete it
 * @author gb
 */
public class TCPSender extends Thread {

    /*
    private String fileName;
    private File file;
    private String from;
    private String remoteUser;
    private ArrayList<String> to;
    private Socket sock;

    public TCPSender(String fileName, File file, String from, ArrayList<String> to, String remoteUser) {
        this.fileName = fileName;
        this.file = file;
        this.from = from;
        this.to = to;
        this.remoteUser = remoteUser;
    }

    public void run() {
        ChatNI.printDebug("TCPSender", "run", "Connecting socket");
        try {
            this.sock = new Socket(ChatNI.extractIpFromUsername(this.remoteUser), ChatNI.receivePort);
        } catch (IOException ex) {
            System.err.println("In TCP_Sender : coul not access remote user");
        }

        //===========================================
        //                FILE PROPOSAL
        //===========================================
        //Getting file length
        Path filepath = Paths.get(file.getAbsolutePath());
        long size = -1;
        try {
            size = Files.size(filepath);
        } catch (IOException ex) {
            System.err.println("In TCP_Sender : coul not get the size of the file");
        }

        //Getting output stream we will write on
        OutputStream os = null;
        try {
            os = this.sock.getOutputStream();
        } catch (IOException ex) {
            System.err.println("In TCP_Sender : coul not access socket output stream");
        }

        //Sending the file proposal
        writeFileProposalToOutputStream(size, from, to, os);

        //===========================================
        //            FILE PROPOSAL ANSWER
        //===========================================       
        //Getting the input stream
        InputStream is = null;
        try {
            is = this.sock.getInputStream();
        } catch (IOException ex) {
            System.err.println("In TCP_Sender : coul not get the input stream");
        }

        //Getting the answer read on the input stream
        boolean fileProposalAnswer = fileTransferAnswerIsOk(is);

        //===========================================
        //            SENDING FILE
        //===========================================       
        //Getting the input stream
        if (fileProposalAnswer) {
            ChatNI.printDebug("TCPSender", "run", "File proposal accepted");
            if ((int) size != size) {
                System.err.println("In TCP_Sender : size is to big");
            }
            writeFileToOutputStream(file, (int) size, os);
        } else {
            ChatNI.printDebug("TCPSender", "run", "File proposal refused");
        }

        ChatNI.printDebug("TCPSender", "run", "Closing connection");

        try {
            this.sock.close();
        } catch (IOException ex) {
            System.err.println("In TCP_Sender : could not close socket");
        }
    }
    */

    /**
     * Reads an input stream to extract a Signal FileTransferOK or
     * FileTransferNOK, return a boolean accordingly
     *
     * @param is
     * @return true if received FileTransferOK false if received FileTransferNOK
     * (default = false + error message)
     */
    /*
    private boolean fileTransferAnswerIsOk(InputStream is) {
        byte[] data = new byte[Signal.MAX_SIZE];
        Signal answer = null;

        try {
            is.read(data);
        } catch (IOException ex) {
            System.err.println("In TCP_Sender - fileTransferAnswer : coul not read is");
        }
        try {
            answer = Signal.fromByteArray(data);
        } catch (IOException ex) {
            System.err.println("In TCP_Sender - fileTransferAnswer : coul not convert data read to Signal");
        } catch (ClassNotFoundException ex) {
            System.err.println("In TCP_Sender - fileTransferAnswer : Signal received is too big");
        }
        if (answer instanceof FileTransferOK) {
            return true;
        } else if (answer instanceof FileTransferNOK) {
            return false;
        } else {
            System.err.println("In TCP_Sender - fileTransferAnswer : Signal received is neither FileTransferOK nor FileTransferNOK");
            return false;
        }
    }
*/
    /**
     * Writes a file proposal to a an output stream
     *
     * @param file
     * @param size
     * @param from
     * @param to
     * @param os
     */
  /*
    private void writeFileProposalToOutputStream(long size, String from, ArrayList<String> to, OutputStream os) {

        ChatNI.printDebug("TCPSender", "writeFileProposalToOutputStream", "Sending file proposal "
                + fileName + "\tsize:" + size
                + "\tfrom:" + from + "\tto" + to);
        FileProposal fp = new FileProposal(fileName, size, from, to);
        try {
            os.write(Signal.toByteArray(fp));
        } catch (SignalTooBigException ex) {
            System.err.println("In TCP_Sender : file proposal is too big");
        } catch (IOException ex) {
            System.err.println("In TCP_Sender : coul not write the file proposal");
        }
    }
*/
    /**
     * Reads a file and writes it inside a output stream
     *
     * @param file
     * @param size
     * @param os
     */
  /*  private void writeFileToOutputStream(File file, int size, OutputStream os) {
        ChatNI.printDebug("TCPSender", "writeFileToOutputStream", "Creating the f.i.s on "
                + fileName);
        FileInputStream fis = null;
        
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            System.err.println("In TCP_Sender - writeFileToOutputStream : coul not create the file Input Stream");
        }
        byte[] fileData = new byte[size];
        try {
            fis.read(fileData);
            os.write(fileData);
            os.flush();
        } catch (IOException ex) {
            System.err.println("In TCP_Sender - writeFileToOutputStream : coul not read the fis");
        }

        /
        try {
            synchronized (this) {
                Thread.currentThread().wait(10000);
            }
        } catch (InterruptedException ex) {
            System.err.println("In TCP_Sender - writeFileToOutputStream : coul not sleep 10 sec");
        }
        * /
        try {
            fis.close();
        } catch (IOException ex) {
            System.err.println("In TCP_Sender - writeFileToOutputStream : coul not close fis and bis");
        }
        ChatNI.printDebug("TCPSender", "writeFileToOutputStream", "File written");

    }
*/
}
